home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1542 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.7 KB  |  56 lines

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Fastest way to index thru an array????
  5. Date: 11 Jan 1996 15:26:00 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan11102600@g7240065.bridge.bst.bls.com>
  8. References: <4d1g3k$o09@solaris.cc.vt.edu>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: Ashutosh Gokhale's message of 10 Jan 1996 22:54:44 GMT
  11.  
  12. In article <4d1g3k$o09@solaris.cc.vt.edu> Ashutosh Gokhale <ashutosh> writes:
  13.  
  14. :   Suppose I have an array A[][][] which I declare as
  15. :       double ***A and then assign memory to it using new operator.
  16. :   Now consider that A has dimensions A[50][60][70]. Then I can index thru ALL
  17. :   entries of A using
  18. :   for(i=1; i<50; i++)
  19. :    {
  20. :      for(j=1; j<60; j++)
  21. :       {
  22. :     for(k=1; k<70; k++)
  23. :      {
  24. :        A[i][j][k] = <some expression>;
  25. :      }
  26. :       }
  27. :    }
  28. :   But the above way is surely the most time-INEFFICIENT way.
  29. :   Can someone suggest me the MOST time efficient way of indexing through A[][][]?
  30.  
  31. The only inefficient bit is the array access, the actual looping touches
  32. every location only once and you can't really get a more efficient access
  33. than that.
  34. You can change the array access to pointer access and do something like
  35. (NB: array indices start at 0)
  36.  
  37.   double*** fred = A;
  38.   for (int i = 0; i < 50; i++) {
  39.       double** wilma = *fred++;
  40.       for (int j = 0; j < 60; j++) {
  41.           double* dino = *wilma++;
  42.           for (int k = 0; k < 70; k++) {
  43.               *dino++ = <some expression>;
  44.           }
  45.       }
  46.   }
  47.  
  48. But this may be totally unnecessary because the compiler could optimise this
  49. code for you.
  50.  
  51. Regards
  52.    -A.
  53.  
  54. -- 
  55. | A.Champion                |
  56.